package opp.China;

import java.util.Scanner;

import java.util.Random;

public class Test {
    public static void main(String[] args){

        //猜字谜游戏

        int count=0;
        //生成0到100以内的随机数
        Random random=new Random();
        int number = random.nextInt(100)+1;


        System.out.println("你有五次机会！");
        while(count<=5) {
            //从键盘输入猜的数字
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你要猜的数字：");
            int guessNumber = scanner.nextInt();

            //比较输入的数字和系统产生的数字
            //猜一猜计数器加一
            if (guessNumber > number) {
                count++;
                System.out.println("你猜的数字大了\n你已猜"+count+"次");
            } else if (guessNumber < number) {
                count++;
                System.out.println("你猜的数字小了\n你已猜"+count+"次");
            } else {
                System.out.println("恭喜你，猜对了！");
                break;
            }
            //计数器到第五次时，机会用完，跳出循环
            if(count==5){
                System.out.println("你的"+count+"次机会已用完！\n答案是："+number);
                break;
            }
        }
    }

    }


